//src/main/java/com/bookmanager/controller/CategoryController.java
package com.bookmanager.controller;

import com.bookmanager.model.Category;
import com.bookmanager.model.User;
import com.bookmanager.service.CategoryService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/admin/book-categories")
public class CategoryController extends HttpServlet {
    private CategoryService categoryService = new CategoryService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        // 检查管理员权限
        if (user == null || !"admin".equals(user.getRole())) {
            response.sendRedirect("../login.jsp");
            return;
        }

        String action = request.getParameter("action");

        if ("edit".equals(action)) {
            showEditForm(request, response);
        } else {
            // 显示分类列表
            request.setAttribute("categories", categoryService.getAllCategories());
            request.getRequestDispatcher("/admin/book-categories.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null || !"admin".equals(user.getRole())) {
            response.sendRedirect("../login.jsp");
            return;
        }

        String action = request.getParameter("action");

        if ("add".equals(action)) {
            addCategory(request, response);
        } else if ("update".equals(action)) {
            updateCategory(request, response);
        } else if ("delete".equals(action)) {
            deleteCategory(request, response);
        }
    }

    private void addCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Category category = new Category();
        category.setCategoryName(request.getParameter("categoryName"));
        category.setDescription(request.getParameter("description"));

        if (categoryService.addCategory(category)) {
            response.sendRedirect("book-categories?message=add_success");
        } else {
            response.sendRedirect("book-categories?message=add_failed");
        }
    }

    private void updateCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));

            Category category = new Category();
            category.setCategoryId(categoryId);
            category.setCategoryName(request.getParameter("categoryName"));
            category.setDescription(request.getParameter("description"));

            if (categoryService.updateCategory(category)) {
                response.sendRedirect("book-categories?message=update_success");
            } else {
                response.sendRedirect("book-categories?message=update_failed");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect("book-categories?message=invalid_id");
        }
    }

    private void deleteCategory(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));

            if (categoryService.deleteCategory(categoryId)) {
                response.sendRedirect("book-categories?message=delete_success");
            } else {
                response.sendRedirect("book-categories?message=delete_failed&error=has_books");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect("book-categories?message=invalid_id");
        }
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            int categoryId = Integer.parseInt(request.getParameter("categoryId"));
            Category category = categoryService.getCategoryById(categoryId);

            if (category != null) {
                request.setAttribute("category", category);
                request.getRequestDispatcher("/admin/category-edit.jsp").forward(request, response);
            } else {
                response.sendRedirect("book-categories?message=category_not_found");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect("book-categories?message=invalid_category_id");
        }
    }
}